﻿using Newtonsoft.Json.Linq;
using StorageApp.APIS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace StorageApp
{
    /// <summary>
    /// _device.xaml 的交互逻辑
    /// </summary>
    public partial class _device : Window
    { 
        public  _device()
        { 
            initUserAsync();
            InitializeComponent();
        }
        public string? device_name;
        public string device_id = "0";

        public   void initUserAsync()
        {  
                setDeviceView(); 
        }
        public async void setDeviceView()
        {
            var res = await api.api_device();
            if(res["data"]!=null)
                setdevice(employee_g, res["data"]["rows"]);
        }
       
        private void setdevice(System.Windows.Controls.GroupBox box, JToken? optor)
        {

            box.Content = null;
            var stackPanel = new WrapPanel();
            foreach (var item in optor)
            {
                var button = new System.Windows.Controls.Button();
    //            < TextBlock TextWrapping = "Wrap" Width = "100" >
    //    这是一段很长的文本，如果不设置TextWrapping为Wrap，它就会超出TextBlock的边界而无法显示完整。
    //</ TextBlock >
                var textblock= new System.Windows.Controls.TextBlock();
                textblock.Text= item["name"].ToString();
                textblock.TextWrapping = System.Windows.TextWrapping.Wrap;
                button.Content = textblock;
                button.Width = 120;
                button.Height = 100;
                button.Margin = new Thickness(5);
                
                PropertyHelper.SetProperty(button, item["id"].ToString());
                button.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF, 0xA9, 0xF6, 0xB8));
              
          
                button.Click += (s, e) => {
                    System.Windows.Controls.Button btn = (System.Windows.Controls.Button)s;
                    string content =((TextBlock) btn.Content).Text;
                    if (content != null)
                    {
                        device_name=content;
                        device_id = PropertyHelper.GetProperty(btn);
                    }
                    this.Close();
                };

                stackPanel.Children.Add(button);
            }
            box.Content = stackPanel;
        }
    }
}
